package specifications;

import java.lang.reflect.Field;

import entities.Aircraft;
import entities.Airline;
import entities.Airport;
import entities.Flight;

public class FlightSpecification implements Specification<Flight> {
	private String attribute;
	private Object value;

	public FlightSpecification(String attribute, Object value) {
		this.attribute = attribute;
		this.value = value;
	}

	@Override
	public final boolean isSatisfiedBy(Flight candidate) {
		Field field = null;
		try {
			field = candidate.getClass().getDeclaredField(this.getAttribute());
		} catch (NoSuchFieldException | SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Object candidateValue = null;
		try {
			field.setAccessible(true);
			candidateValue = field.get(candidate);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isSatisfiedByValue(candidateValue);
	}

	protected boolean isSatisfiedByValue(Object candidateValue) {
		if (this.getAttribute() == "flightAirline") {
			Airline c = (Airline) candidateValue;
			return c.getAirlineName().equalsIgnoreCase((String) getValue());
		} else {
			if (this.getAttribute() == "flightAircraft") {
				Aircraft c = (Aircraft) candidateValue;
				return c.getAircraftType()
						.equalsIgnoreCase((String) getValue());
			} else {
				if ((this.attribute == "departureAirport")
						|| (this.attribute == "arriveAirport")) {
					Airport c = (Airport) candidateValue;
					return c.getAirportName().equalsIgnoreCase(
							(String) getValue());
				} else {
					if (this.attribute == "departureDateTime")
					{
					
						String s = (String) candidateValue ;
						return s.contains((String) getValue());

					}else {
						return candidateValue.equals(getValue());
					}
				}
			}
		}
	}

	public String getAttribute() {
		return attribute;
	}

	public Object getValue() {
		return value;
	}
}
